package com.sky.controller.user;

import com.sky.dto.AddressBookSetDefaultDTO;
import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/user/addressBook")
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;
    @PostMapping
    public Result<String> save(@RequestBody AddressBook addressBook){
        log.info("新增地址");
        addressBookService.save(addressBook);
        return Result.success();
    }

    @GetMapping("/list")
    public Result<List<AddressBook>> list(){
        log.info("查询地址");
        List<AddressBook> list = addressBookService.list();
        return Result.success(list);
    }

    @GetMapping("/default")
    public Result<AddressBook> getDefault(){
        log.info("查询默认地址");
        AddressBook addressBook = addressBookService.getDefault();
        return Result.success(addressBook);
    }

    @GetMapping("/{id}")
    public Result<AddressBook> getById(@PathVariable Long id){
        log.info("查询地址");
        AddressBook addressBook = addressBookService.getById(id);
        return Result.success(addressBook);
    }
    @PutMapping
    public Result<String> update(@RequestBody AddressBook addressBook){
        log.info("修改地址");
        addressBookService.update(addressBook);
        return Result.success();
    }

    @PutMapping("/default")
    public Result<String> setDefault(@RequestBody AddressBookSetDefaultDTO id){
        log.info("设置默认地址");
        addressBookService.setDefault(id.getId());
        return Result.success();
    }
}
